$('#keyword_search').live('submit', function(e) {
	redirectAppend("keywords", $('#keywords').val());
	e.preventDefault();
});

$('#stream_link').live('click', function(e) {
	var urlVariables 	= getUrlVars(),
		url				= $(this).attr('href'),
		newUrl			= setUrlVars(urlVariables, url);
	window.location.replace(newUrl);
	e.preventDefault();
});

$('.keyword_tagcloud').live('click', function(e) {	
	redirectAppend("keywords", $(this).html());
	e.preventDefault();
});

$('.tag_button').live('click', function(e) {
	var id = $(this).attr('id');
	var split = id.split('_');
	redirectRemove("keywords", split[1]);
	e.preventDefault();
});

$('.tag_button_deactivated').live('click', function(e) {
	
});

$('.remove_tag_button').live('click', function(e) {
	
});


/**
 * This replaces the value to the key in the GET variables,
 * 			before it redirects to the new page.
 * 
 * @param key: the key that is used in the url
 * @param val: the value that is to be replaced for the key
 */
function redirectReplace(key, val) {
	var urlVariables 	= getUrlVars(),
		values 			= val.split(" ");
	
	if($.inArray(key, urlVariables) > -1) {
		urlVariables[key] = values;
		url = setUrlVars(urlVariables);
	} else {
		urlVariables.push(key);
		urlVariables[key] = values;
		url = setUrlVars(urlVariables);
	}
	window.location.replace(url);
}

/**
 * This appends the value to the key in the GET variables,
 * 			before it redirects to the new page.
 * 
 * @param key: the key that is used in the url
 * @param val: the value that is to be appended to the key
 */
function redirectAppend(key, val) {
	var urlVariables 	= getUrlVars(),
		values 			= val.split(" ");
	
	console.log(urlVariables);
	
	if($.inArray(key, urlVariables) > -1) {
		$.each(values, function(i, v) {
			if($.inArray(values[i], urlVariables[key]) === -1){
				urlVariables[key].push(values[i]);
			}
		});
		url = setUrlVars(urlVariables);
	} else {
		urlVariables.push(key);
		urlVariables[key] = values;
		url = setUrlVars(urlVariables);
	}
	window.location.replace(url);
}

/**
 * This removes the value to the key in the GET variables,
 * 			before it redirects to the new page.
 * 
 * @param key: the key that is used in the url
 * @param val: the value that is to be appended to the key
 */

function redirectRemove(key, val) {
	var urlVariables	= getUrlVars();
		values			= val.split(" ");
	
		if($.inArray(key, urlVariables) > -1) {
			
			var keywords = urlVariables[key];
			var index = $.inArray(val, keywords);
			
			keywords.splice(index,1);
			
			url = setUrlVars(urlVariables);
		} 
	window.location.replace(url);
		
}

/**
 * Returns the variables that are in the url at this moment.
 * 
 * @return vars in this format:
 * 		{
 *   		"var_name"    	: ["value1"],
 *   		"var_name2" 	: ["value1", "value2"]
 *		}
 */
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    try {
        for(var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            hash[1] = hash[1].split('+');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
    } catch(e) {
    }

    return vars;
}

/**
 * Sets the url variables to match a given array of variables
 * 
 * @param vars in this format:
 * 		{
 *   		var_name    	: ["value1"],
 *   		var_name2 		: ["value1", "value2"]
 *		}
 */
function setUrlVars(vars, url) {
	if(url === undefined){
		var url = window.location.href.slice(0, window.location.href.indexOf('?'));
	}
	var bits = [];
	
	
	$.each(vars, function(index, value) {
		if( vars[value].length > 0) {
			bits.push(value + '=' + vars[value].join('+'));
		}
	});
	
	return bits.length > 0 ? url + '?' + bits.join('&') : url;
}